package com.liuheng.manager.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liuheng.manager.service.CategoryService;
import com.liuheng.manager.utils.ExcelListener;
import com.liuheng.spzx.model.entity.product.Category;
import com.liuheng.spzx.model.vo.common.Result;
import com.liuheng.spzx.model.vo.common.ResultCodeEnum;
import com.liuheng.spzx.model.vo.product.CategoryExcelVo;
import jakarta.servlet.http.HttpServletResponse;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/findByParentId/{parentId}")
    public Result<List<Category>> findByParentId(@PathVariable Long parentId){
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getParentId,parentId);
        List<Category> list = categoryService.list(wrapper);
        for (Category category : list) {
            Long id = category.getId();
            LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Category::getParentId,id);
            long count = categoryService.count(lambdaQueryWrapper);
            if (count>0)
            {
                    category.setHasChildren(true);
            }else {
                category.setHasChildren(false);
            }
        }
       return Result.build(list, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/exportData")
    public void exportData(HttpServletResponse response){
          response.setContentType("application/vnd.ms-excel");
          response.setCharacterEncoding("UTF-8");
        try {
            String fileName = URLEncoder.encode("分类数据", "UTF-8");
            response.setHeader("Content-Disposition","attachment;filename="+fileName+".xls");
            List<Category> list = categoryService.list();
            ArrayList<CategoryExcelVo> categoryExcelVos = new ArrayList<>();
            for (Category category : list) {
                CategoryExcelVo categoryExcelVo = new CategoryExcelVo();
                BeanUtils.copyProperties(category,categoryExcelVo);
                categoryExcelVos.add(categoryExcelVo);
            }
            EasyExcel.write(response.getOutputStream(), CategoryExcelVo.class).sheet("分类数据").doWrite(categoryExcelVos);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    @PostMapping("importData")
    public Result importData(@RequestParam("file") MultipartFile file){
//        categoryService.importData(file);
        try {
            ExcelListener<CategoryExcelVo> excelListener = new ExcelListener<CategoryExcelVo>(categoryService);
            EasyExcel.read(file.getInputStream(), CategoryExcelVo.class,excelListener).sheet().doRead();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}

